"""
Copyright (c) 2022 Huawei Technologies Co.,Ltd.

openGauss is licensed under Mulan PSL v2.
You can use this software according to the terms and conditions of the Mulan PSL v2.
You may obtain a copy of Mulan PSL v2 at:

          http://license.coscl.org.cn/MulanPSL2

THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
See the Mulan PSL v2 for more details.
"""
"""
Case Type   : 用户-alter user
Case Name   : 使用非法字符或语法alter user，合理报错
Create At   : 2023/02/23
Owner       : @kyeleze
Description :
    1.创建用户
    2.使用无引号密码字符修改用户密码
    3.使用account lock+password错误语法
    4.使用无account的lock语法
Expect      :
    1.用户创建成功
    2.用户修改失败，合理报错
    3.用户修改失败，合理报错
    4.用户修改失败，合理报错
History     :
"""

import unittest
import os

from yat.test import macro

from testcase.utils.CommonSH import CommonSH
from testcase.utils.CommonSH import Constant
from testcase.utils.Logger import Logger


class UserTestCase(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.log.info(f'----{os.path.basename(__file__)} start----')
        self.primary_sh = CommonSH('PrimaryDbUser')
        self.constant = Constant()
        self.user_name = 'u_alter_user_0013'

    def test_alter_user(self):
        text = '--step1:创建用户 expect:成功--'
        self.log.info(text)
        sql_cmd = f"drop user if exists {self.user_name};" \
                  f"create user {self.user_name} with password " \
                  f"'{macro.COMMON_PASSWD}';"
        self.log.info(sql_cmd)
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.CREATE_ROLE_SUCCESS_MSG,
                      result, '清理环境失败')

        text = '--step2:使用无引号密码字符修改用户密码 ' \
               'expect:用户修改失败，合理报错--'
        self.log.info(text)
        sql_cmd1 = f'alter user {self.user_name} identified by aswd3456 ' \
                   f'replace dfg1637484kkkkkkkkkkkkkkkkkkkkkkkk' \
                   f'kkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkklll;'
        self.log.info(sql_cmd1)
        result = self.primary_sh.execut_db_sql(sql_cmd1)
        self.log.info(result)
        self.assertIn('ERROR:  Password must be quoted',
                      result, '执行失败' + text)

        text = '--step3:使用account lock password错误语法修改用户 ' \
               'expect:用户修改失败，合理报错--'
        sql_cmd2 = f'alter user {self.user_name} account lock password ' \
                   f'expire never  password expire never account unlock  ' \
                   f'account unlock account unlock account unlock'
        self.log.info(sql_cmd2)
        result = self.primary_sh.execut_db_sql(sql_cmd2)
        self.log.info(result)
        self.assertIn('ERROR:  syntax error at or near "password',
                      result, '执行失败' + text)
        text = '--step4:使用无account的lock语法 ' \
               'expect:用户修改失败，合理报错--'
        sql_cmd3 = f'alter user {self.user_name} lock password expir never ' \
                   f'lock password expire never lock password ' \
                   f'expire never lock;'
        self.log.info(sql_cmd3)
        result = self.primary_sh.execut_db_sql(sql_cmd3)
        self.log.info(result)
        self.assertIn('ERROR:  syntax error at or near "lock"',
                      result, '执行失败' + text)

    def tearDown(self):
        self.log.info('--清理环境--')
        sql_cmd = f'drop user {self.user_name};'
        result = self.primary_sh.execut_db_sql(sql_cmd)
        self.log.info(result)
        self.assertIn(self.constant.DROP_ROLE_SUCCESS_MSG,
                      result, '清理环境失败')
        self.log.info(f'----{os.path.basename(__file__)} end----')

